package com.atguigu.rabbitmq.topic;

import com.atguigu.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author 杨林
 * @create 2022-10-07 16:00 星期五
 * description:这是通配模式下的生产者
 */
public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //获取连接
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        //创建交换机
        channel.exchangeDeclare("topic_exchange", BuiltinExchangeType.TOPIC,true,false,false,null);

        //创建两个队列
        channel.queueDeclare("topic1_queue", true, false, false, null);
        channel.queueDeclare("topic2_queue", true, false, false, null);

        //绑定队列和交换机
        /**
         * 参数说明：
         *      queue:队列名称
         *      exchange:交换机名称
         *      routingKey:路由键  绑定规则
         *          如果交换机的列行为fanout,routingKey设置为""
         */
        //需求：所有error级别的日志绑定队列1
        //      所有订单级别的日志绑定队列2
        channel.queueBind("topic1_queue", "topic_exchange", "#.error");
        channel.queueBind("topic2_queue", "topic_exchange", "order.*");
        channel.queueBind("topic2_queue", "topic_exchange", "*.*");

        //发送消息
        String msg1 = "这是error级别的日志";
        String msg2 = "这是订单的日志";

        channel.basicPublish("topic_exchange", "order.info", null, msg2.getBytes());
        channel.basicPublish("topic_exchange", "exception.IOException.error", null, msg1.getBytes());

        //关闭资源
        channel.close();
        connection.close();
    }
}
